//#include "zb_common.h"
// -----------------------------------------------------------------------------
// INCLUDES
// -----------------------------------------------------------------------------
#include "./emkit_comm/emkit.h"
#include "app/framework/include/af.h"
#include "app/framework/plugin-soc/connection-manager/connection-manager.h"
#include EMBER_AF_API_BUTTON_INTERFACE
#include EMBER_AF_API_LED_BLINK
#include "util/attribute-storage.h"

#include "./singleBlink_ctl.h"
#include "./scene_config.h"
#include "./battery_ctl.h"
#include "z3_extend/ext_scene.h"
#include "z3_extend/ext_bind.h"
#include "z3_extend/ext_report.h"
#include "MLR.h"
#include "em_device.h"
#include "em_gpio.h"
#include "Ext_Report.h"
#include "main_ui.h"

#if SIMON_ENABLE
extern void light_blink_start(u8 times, u16 ledOnTime, u16 ledOffTime);
#include "zcl_const.h"
extern _CODE_ZCL_ void reportAttr_simon(u8 ep,u16 cluster_id,u16 attrid);
//#include "zcl_onoff.h"
#endif
//simon lixp 20200409 add light function
// ��ʱ����������ƽ����������
//extern ev_time_event_t *tl_zbTimerTaskPost(ev_timer_callback_t func, void *arg, u32 t_us);
static void _Local_DrvWr8bits(uint8_t dat);
#if SIMON_ENABLE
extern u8 g_simon_slight_status;
extern void light_blink_stop(void);
#endif
//simon lixp 20200409 add light function
/* -- IO ���ձ� --
 * load0  load1 load2 load3 
 *  |    |     |     |  
 * PB2   PB3       PB4    PB5 
 *  4 1   4 0        3 1        3 0
 */
EmberEventControl mlr_contral_ec;
void mlr_contral_ef(void)
{
	//UTILS_PRINT(UPM_KEY, "222222\n");
	emberEventControlSetInactive(mlr_contral_ec);
	//simon_led_off(0);
  _Local_DrvWr8bits(0);

#if 0
  if(g_simon_slight_status==JOINING_NET){
    light_blink_stop();
    light_blink_start(250, 125, 125);
    //led_on(LED1);
    bdb_networkSteerStart();
  }else if(g_simon_slight_status==LEAVEING_NET){
    zb_factoryReset();
    g_simon_slight_status=NOMAL_STATUS;
    light_blink_stop();
    light_blink_start(12, 250, 250);

  }
#endif
  //simon lixp 20200409 add light function
  return ;
}


static void _Local_DrvWr8bits(uint8_t dat){
#if SIMON_KEY3_ENABLE
  if(dat&(1 << 5)){
	  GPIO_PinOutSet(gpioPortA, 5);
	}else{
		GPIO_PinOutClear(gpioPortA, 5);
	}
	if(dat&(1 << 4)){
		GPIO_PinOutSet(gpioPortA, 6);
	}else{
		GPIO_PinOutClear(gpioPortA, 6);
	}
  if(dat&(1 << 3)){
	  GPIO_PinOutSet(gpioPortA, 3);
	}else{
		GPIO_PinOutClear(gpioPortA, 3);
	}
	if(dat&(1 << 2)){
		GPIO_PinOutSet(gpioPortA, 4);
	}else{
		GPIO_PinOutClear(gpioPortA, 4);
	}
  if(dat&(1 << 1)){
	  GPIO_PinOutSet(gpioPortA, 1);
  }else{
	  GPIO_PinOutClear(gpioPortA, 1);
  }
    if(dat&(1 << 0)){
    	GPIO_PinOutSet(gpioPortA, 2);
  }else{
	  GPIO_PinOutClear(gpioPortA, 2);
  }

#else //if SIMON_KEY2_ENABLE
  if(dat&(1 << 3)){
	  GPIO_PinOutSet(gpioPortF, 2);
	}else{
		GPIO_PinOutClear(gpioPortF, 2);
	}
	if(dat&(1 << 2)){
		GPIO_PinOutSet(gpioPortC, 8);
	}else{
		GPIO_PinOutClear(gpioPortC, 8);
	}
  if(dat&(1 << 1)){
	  GPIO_PinOutSet(gpioPortB, 11);
  }else{
	  GPIO_PinOutClear(gpioPortB, 11);
  }
    if(dat&(1 << 0)){
    	GPIO_PinOutSet(gpioPortB, 15);
  }else{
	  GPIO_PinOutClear(gpioPortB, 15);
  }
  /*
 #else
  if(dat&(1 << 3)){
      gpio_write(GPIO_PB5, 1);
  }else{
    gpio_write(GPIO_PB5, 0);
  }
  if(dat&(1 << 2)){
    gpio_write(GPIO_PB4, 1);
  }else{
    gpio_write(GPIO_PB4, 0);
  } 
  */

#endif
  



}


void _DrvInit_MLR(void){
#if 0 
  CMU_ClockEnable(cmuClock_GPIO, true);
  GPIO_PinModeSet(gpioPortB, 15, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortB, 11, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortC, 8, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortC, 9, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortF, 2, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortC, 11, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortD, 15, gpioModePushPull, 0);
  GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 0);
#endif 
#if SIMON_KEY3_ENABLE
  gpio_set_func(GPIO_PA1,AS_GPIO);
  gpio_set_output_en(GPIO_PA1, 1);    //enable output
  //gpio_set_input_en(GPIO_PA1 ,0);     //disable input

  gpio_set_func(GPIO_PA2,AS_GPIO);
  gpio_set_output_en(GPIO_PA2, 1);    //enable output
  //gpio_set_input_en(GPIO_PA2 ,0);     //disable input

  gpio_set_func(GPIO_PA3 ,AS_GPIO);
	gpio_set_output_en(GPIO_PA3, 1); 		//enable output
	//gpio_set_input_en(GPIO_PA3 ,0);			//disable input

	gpio_set_func(GPIO_PA4 ,AS_GPIO);
	gpio_set_output_en(GPIO_PA4, 1); 		//enable output
  

  gpio_set_func(GPIO_PA5 ,AS_GPIO);
  gpio_set_output_en(GPIO_PA5, 1);    //enable output
  //gpio_set_input_en(GPIO_PA5 ,0);     //disable input

  gpio_set_func(GPIO_PA6 ,AS_GPIO);
  gpio_set_output_en(GPIO_PA6, 1);    //enable output
  //gpio_set_input_en(GPIO_PA6 ,0);     //disable input 
  
//  gpio_write(GPIO_PA3, 1);                //LED On

#else
  //gpio_set_func(GPIO_PB2 ,AS_GPIO);
  //gpio_set_output_en(GPIO_PB2, 1);    //enable output
  GPIO_PinModeSet(gpioPortB,11,gpioModePushPull,0);
  //gpio_set_input_en(GPIO_PB2 ,0);     //disable input

  //gpio_set_func(GPIO_PB3 ,AS_GPIO);
  //gpio_set_output_en(GPIO_PB3, 1);    //enable output
  GPIO_PinModeSet(gpioPortB,15,gpioModePushPull,0);
  //gpio_set_input_en(GPIO_PB3 ,0);     //disable input
  //gpio_set_func(GPIO_PC2 ,AS_GPIO);
	//gpio_set_output_en(GPIO_PC2, 1); 		//enable output
	//gpio_set_input_en(GPIO_PC2 ,0);			//disable input
	GPIO_PinModeSet(gpioPortC,8,gpioModePushPull,0);

	//gpio_set_func(GPIO_PC4 ,AS_GPIO);
	//gpio_set_output_en(GPIO_PC4, 1); 		//enable output
	GPIO_PinModeSet(gpioPortF,2,gpioModePushPull,0);

  //gpio_set_input_en(GPIO_PB5 ,0);     //disable input 
  
//  gpio_write(GPIO_PA3, 1);                //LED On

#endif
  _Local_DrvWr8bits(0);   // �رռ̵�������
}


// 7531
// 6420
void _DrvWr_MLR(uint8_t ch, uint8_t newStatus){

  uint8_t _outDat = 0xFF;
  uint8_t _sp_h = 0;
  uint8_t _sp_l = 0;

  if(ch == 1){
    _sp_h = 7;
    _sp_l = 6;
  }
  else if(ch == 2){
    _sp_h = 5;
    _sp_l = 4;

    if(emberAfNetworkState() == EMBER_JOINED_NETWORK){

    }
  }
  else if(ch == 3){
    _sp_h = 3;
    _sp_l = 2;
    if(emberAfNetworkState() == EMBER_JOINED_NETWORK){
		//_writeAttr_OnOff(SAMPLE_SWITCH_ENDPOINT,1);
    	_reportStdAttribute(0x0000, SAMPLE_SWITCH_ENDPOINT, 1, 0x0006, 0x0000, 1);
    }
  }
  else if(ch == 4){
    _sp_h = 1;
    _sp_l = 0;
    if(emberAfNetworkState() == EMBER_JOINED_NETWORK){
      //reportAttr_simon(0x02,ZCL_CLUSTER_GEN_ON_OFF,0x0000);
      _reportStdAttribute(0x0000, SAMPLE_SWITCH_KEY2_ENDPOINT, 1, 0x0006, 0x0000, 1);
    }
  }
  else{
    return;
  }

  if(newStatus){
    _outDat &= ~(1 << _sp_h);
  }
  else{
    _outDat &= ~(1 << _sp_l);
  }
  _Local_DrvWr8bits(~_outDat);
  //tl_zbTimerTaskPost(smTimeoutCb, NULL, _MLR_INTERVAL);
  //UTILS_PRINT(UPM_KEY, "111111\n");
  emberEventControlSetDelayMS(mlr_contral_ec, _MLR_INTERVAL);
  //printf("[Drv]ch %d, dat %d, outDat[0x%X]\n", ch, newStatus, _outDat);

}

// 7531
// 6420
void _DrvWr_MLR_All(uint8_t newStatusMask){
#if 0
//return;
  uint8_t _outDat = 0xFF;

  if(newStatusMask & (1 << 1)){
    _outDat &= ~(1 << 7);
  }
  else{
    _outDat &= ~(1 << 6);
  }
#if (CHANNEL_NUM > 1)
  if(newStatusMask & (1 << 2)){
    _outDat &= ~(1 << 5);
  }
  else{
    _outDat &= ~(1 << 4);
  }
#if (CHANNEL_NUM > 2)
  if(newStatusMask & (1 << 3)){
    _outDat &= ~(1 << 3);
  }
  else{
    _outDat &= ~(1 << 2);
  }
#if (CHANNEL_NUM > 3)
  if(newStatusMask & (1 << 4)){
    _outDat &= ~(1 << 1);
  }
  else{
    _outDat &= ~(1 << 0);
  }
#endif
#endif
#endif

  _Local_DrvWr8bits(~_outDat);

  // ��ʱ����������ƽ����������
  extern EmberEventControl _DrvMLR_PinSet_EC;
  emberEventControlSetDelayMS(_DrvMLR_PinSet_EC, _MLR_INTERVAL);

  DEBUG("[Drv]all --wr, outDat[0x%X]\n", _outDat);
#endif  
}

#if 0
EmberEventControl _DrvMLR_PinSet_EC;
void _DrvMLR_PinSet_EF(void){
  emberEventControlSetInactive(_DrvMLR_PinSet_EC);

  _Local_DrvWr8bits(0); // �ر����м̵�������
  DEBUG("[Drv]all channel off 0xFF");
}
#endif



